<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
    <th:block th:include="include :: header('新增出库单')" />
    <th:block th:include="include :: datetimepicker-css" />
</head>
<body class="white-bg">
    <div class="wrapper wrapper-content animated fadeInRight ibox-content">
        <form class="form-horizontal m" id="form-outbound-add">
            <h4 class="form-header h4" th:text="#{outboundInformation}">出库单信息</h4>
            <div class="row">
            <div class="col-xs-6">
                <div class="form-group">
                    <label class="col-sm-4 control-label is-required">[[#{warehouse}]]：</label>
                    <div class="col-sm-8">
                        <div class="input-group">
                            <input name="warehouseId" class="form-control" type="hidden">
                            <input id="suggest-warehouse" class="form-control" type="text" required>
                            <div class="input-group-btn">
                                <button type="button" class="btn btn-white dropdown-toggle" data-toggle="dropdown">
                                    <span class="caret"></span>
                                </button>
                                <ul class="dropdown-menu dropdown-menu-right" role="menu">
                                </ul>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            <div class="col-xs-6">
                <div class="form-group">
                    <label class="col-sm-4 control-label is-required">[[#{documentNumber}]]：</label>
                    <div class="col-sm-8">
                        <input name="outboundOrderNo" th:value="${outboundOrderNo}" class="form-control" type="text" required readonly>
                    </div>
                </div>
            </div>
            <div class="col-xs-6">
                <div class="form-group">
                    <label class="col-sm-4 control-label">[[#{associatedOrderNumber}]]：</label>
                    <div class="col-sm-8">
                        <input name="relOrderNo" class="form-control" type="text">
                    </div>
                </div>
            </div>
            <div class="col-xs-6">
                <div class="form-group">
                    <label class="col-sm-4 control-label">[[#{documentDate}]]：</label>
                    <div class="col-sm-8">
                        <div class="input-group date">
                            <input name="documentDate" class="form-control" placeholder="yyyy-MM-dd" type="text">
                            <span class="input-group-addon"><i class="fa fa-calendar"></i></span>
                        </div>
                    </div>
                </div>
            </div>
            <div class="col-xs-6">
                <div class="form-group">
                    <label class="col-sm-4 control-label">[[#{outboundType}]]：</label>
                    <div class="col-sm-8">
                        <select name="outboundType" class="form-control" th:with="type=${@dict.getType('jt_outbound_type')}">
                            <option th:each="dict : ${type}" th:text="${dict.dictLabel}" th:value="${dict.dictValue}"></option>
                        </select>
                    </div>
                </div>
            </div>
            <div class="col-xs-6">
                <div class="form-group">
                    <label class="col-sm-4 control-label">[[#{remark}]]：</label>
                    <div class="col-sm-8">
                        <textarea name="remark" class="form-control" rows="3"></textarea>
                    </div>
                </div>
            </div>
            </div>
            <h4 class="form-header h4" th:text="#{outboundDetails}">出库单明细信息</h4>
            <div class="row">
                <div class="col-xs-12">
                    <button type="button" class="btn btn-white btn-sm" onclick="selectProductToParentCallBack()"><i class="fa fa-plus"> [[#{add}]]</i></button>
                    <button type="button" class="btn btn-white btn-sm" onclick="sub.delRow()"><i class="fa fa-minus"> [[#{delete}]]</i></button>
                    <div class="col-sm-12 select-table table-striped">
                        <table id="bootstrap-table"></table>
                    </div>
                </div>
            </div>
        </form>
    </div>
    <th:block th:include="include :: footer" />
    <th:block th:include="include :: bootstrap-suggest-js" />
    <th:block th:include="include :: datetimepicker-js" />
    <script th:inline="javascript">
        var prefix = ctx + "business/outbound"
        $("#form-outbound-add").validate({
            focusCleanup: true
        });

        function submitHandler() {
            if ($.validate.form()) {
                $.operate.save(prefix + "/add", $('#form-outbound-add').serialize());
            }
        }

        $("input[name='documentDate']").datetimepicker({
            format: "yyyy-mm-dd",
            minView: "month",
            autoclose: true
        });

        $(function() {
            var options = {
                pagination: false,
                showSearch: false,
                showRefresh: false,
                showToggle: false,
                showColumns: false,
                sidePagination: "client",
                columns: [{
                    checkbox: true
                },
                {
                    field: 'index',
                    align: 'center',
                    title: i18n('serialNumber'),
                    formatter: function (value, row, index) {
                    	var columnIndex = $.common.sprintf("<input type='hidden' name='index' value='%s'>", $.table.serialNumber(index));
                    	return columnIndex + $.table.serialNumber(index);
                    }
                },
                {
                    field: 'productId',
                    align: 'center',
                    title: i18n('productName'),
                    formatter: function(value, row, index) {
                        var html = $.common.sprintf("<input class='form-control' type='hidden' name='jtOutboundOrderItemList[%s].productId' value='%s'>", index, value);
                        return html + row.productName;
                    }
                },
                {
                    field: 'productCode',
                    align: 'center',
                    title: i18n('sku'),
                    formatter: function(value, row, index) {
                        var html = $.common.sprintf("<input class='form-control' type='text' name='jtOutboundOrderItemList[%s].productCode' value='%s'>", index, value);
                        return html;
                    }
                },
                {
                    field: 'styleNo',
                    align: 'center',
                    title: i18n('spu'),
                    formatter: function(value, row, index) {
                        var html = $.common.sprintf("<input class='form-control' type='text' name='jtOutboundOrderItemList[%s].styleNo' value='%s'>", index, value);
                        return html;
                    }
                },
                {
                    field: 'spec',
                    align: 'center',
                    title: i18n('spec')
                },
                {
                    field: 'unitPrice',
                    align: 'center',
                    title: i18n('unitPrice'),
                    formatter: function(value, row, index) {
                        var html = $.common.sprintf("<input class='form-control' type='text' name='jtOutboundOrderItemList[%s].unitPrice' value='%s'>", index, value);
                        return html;
                    }
                },
                {
                    field: 'qty',
                    align: 'center',
                    title: i18n('outboundQuantity'),
                    formatter: function(value, row, index) {
                        var html = $.common.sprintf("<input class='form-control' type='text' name='jtOutboundOrderItemList[%s].qty' value='%s'>", index, value);
                        return html;
                    }
                },
                {
                    field: 'amount',
                    align: 'center',
                    title: i18n('outboundAmount'),
                    formatter: function(value, row, index) {
                        var html = $.common.sprintf("<input class='form-control' type='hidden' name='jtOutboundOrderItemList[%s].amount' value='%s'>", index, value);
                        return html + value;
                    }
                },
                {
                    title: i18n('operate'),
                    align: 'center',
                    formatter: function(value, row, index) {
                        var value = $.common.isNotEmpty(row.index) ? row.index : $.table.serialNumber(index);
                        return '<a class="btn btn-danger btn-xs" href="javascript:void(0)" onclick="sub.delRowByIndex(\'' + value + '\')"><i class="fa fa-remove"></i>'+i18n('delete')+'</a>';
                    }
                }]
            };
            $.table.init(options);
        });

        function addRow(productInfo) {
            var costPrice = productInfo.cost;

            var qty = productInfo.quantity;
            if(qty == null){
                qty = 1;
            }
            var subAmount = qty * costPrice;

            var count = $("#" + table.options.id).bootstrapTable('getData').length;
            var row = {
                index: $.table.serialNumber(count),
                productId: productInfo.productId,
                productCode: productInfo.productCode,
                productName:productInfo.productName,
                styleNo: productInfo.styleNo,
                spec: productInfo.spec,
                unitPrice: costPrice,
                qty: qty,
                amount: subAmount
            }
            sub.addRow(row);
        }

        var productPrefix = ctx + "business/product";
        // callBack获取父窗口方法
        function selectProductToParentCallBack(){
            var options = {
                title: i18n('selectProduct'),
                url: productPrefix + "/purchase/select",
                callBack: doSubmit2
            };
            $.modal.openOptions(options);
        }

        function doSubmit2(index, layero){
            var selectedRow = layero.find("iframe")[0].contentWindow.getSelections();
            if (selectedRow.length == 0) {
                $.modal.alertWarning(i18n('selectOneProduct'));
                return;
            }

            selectedRow.forEach(function(row) {
                addRow(row);
            });

            $.modal.close(index);
        }
    </script>
    <script th:inline="javascript">
        var warehouseSuggest = $("#suggest-warehouse").bsSuggest({
            url: ctx + "business/warehouse/select/list",
            idField: "warehouseId",
            keyField: "warehouseName",
            clearable: true,
            ignorecase: true,
            hideOnSelect: true,
            effectiveFields: ["warehouseId", "warehouseName"],
            effectiveFieldsAlias: {warehouseId: "仓库ID", warehouseName: "仓库"},
        }).on('onSetSelectValue', function (e, selectedData, selectedRawData) {
            console.log('onSetSelectValue: ', selectedData);

            $("input[name='warehouseId']").val(selectedRawData.warehouseId);
        }).on('onUnsetSelectValue', function (e) {
            //console.log("onUnsetSelectValue=="+e);
            $("input[name='warehouseId']").val("");
        });
    </script>
    <script th:inline="javascript">
        var ROOT = [[${#servletContext.contextPath}]];
        var LANG_COUNTRY = [[${#locale.language+'_'+#locale.country}]];

        //初始化i18n
        $.i18n.properties({
            path: ROOT + '/i18n/',//这里表示访问路径
            name: 'messages',//文件名开头
            language: LANG_COUNTRY,//文件名语言 例如en_US
            mode: 'map'//默认值
        });

        //i18n函数
        function i18n(msgKey) {
            try {
                return $.i18n.prop(msgKey);
            } catch (e) {
                return msgKey;
            }
        }
    </script>
</body>
</html>